/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.fatecpg.passagem;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Mychella
 */
public class calculoPassagemMIDlet extends MIDlet implements CommandListener,ItemStateListener {

    private boolean midletPaused = false;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form form;
    private TextField valorTextField;
    private ChoiceGroup estudanteChoiceGroup;
    private TextField quantidadeTextField;
    private Spacer spacer;
    private StringItem resultadoStringItem;
    private Spacer spacer1;
    private TextField viagemtextField;
    private Form formApresentacao;
    private Spacer spacer2;
    private StringItem descricaoStringItem;
    private ImageItem bannerImageItem;
    private Spacer spacer3;
    private Command sairCommand;
    private Command limparCommand;
    private Command exitCommand;
    private Command calcularCommand;
    private Font font;
    private Image logoimage;
//</editor-fold>//GEN-END:|fields|0|


    /**
     * The calculoPassagemMIDlet constructor.
     */
    public calculoPassagemMIDlet() {
    }

      /**
       * Método para limpar as caixas de texto 
       */
    private void Limpar(){
        valorTextField.setString("");
        viagemtextField.setString("");
        quantidadeTextField.setString("");
        resultadoStringItem.setText("");

    }

     /**
      * 
      * @param valor
      * @return valor no formato de moeda R$
      */
     public static String formatacao(double valor) { // método para formatar com duas casas decimais
        double n = valor;
        int n1 = (int) n;
        int n2 = (int) ((n - n1) * 100);
        String saída = "R$ " + n1 + "," + n2;
        
        return saída;
    }
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getFormApresentacao());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display;
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("C\u00E1lculo de Passagem", new Item[]{getValorTextField(), getViagemtextField(), getQuantidadeTextField(), getEstudanteChoiceGroup(), getSpacer(), getSpacer1(), getResultadoStringItem()});//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getLimparCommand());
            form.addCommand(getSairCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
            form.setItemStateListener(this);
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: valorTextField ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initialized instance of valorTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getValorTextField() {
        if (valorTextField == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            valorTextField = new TextField("Valor da passagem", "", 32, TextField.DECIMAL);//GEN-LINE:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return valorTextField;
    }
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: quantidadeTextField ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of quantidadeTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getQuantidadeTextField() {
        if (quantidadeTextField == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            quantidadeTextField = new TextField("Quantidade de dias", "", 32, TextField.NUMERIC);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return quantidadeTextField;
    }
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: estudanteChoiceGroup ">//GEN-BEGIN:|19-getter|0|19-preInit
    /**
     * Returns an initialized instance of estudanteChoiceGroup component.
     *
     * @return the initialized component instance
     */
    public ChoiceGroup getEstudanteChoiceGroup() {
        if (estudanteChoiceGroup == null) {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
            estudanteChoiceGroup = new ChoiceGroup("Estudante", Choice.EXCLUSIVE);//GEN-BEGIN:|19-getter|1|19-postInit
            estudanteChoiceGroup.append("Sim", null);
            estudanteChoiceGroup.append("N\u00E3o", null);
            estudanteChoiceGroup.setSelectedFlags(new boolean[]{false, false});//GEN-END:|19-getter|1|19-postInit
            // write post-init user code here
        }//GEN-BEGIN:|19-getter|2|
        return estudanteChoiceGroup;
    }
//</editor-fold>//GEN-END:|19-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultadoStringItem ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of resultadoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getResultadoStringItem() {
        if (resultadoStringItem == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            resultadoStringItem = new StringItem("Voc\u00EA deve colocar:", "");//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return resultadoStringItem;
    }
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initialized instance of spacer component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 1);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return spacer;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initialized instance of spacer1 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 1);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return spacer1;
    }
//</editor-fold>//GEN-END:|24-getter|2|





//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|38-preAction
            if (command == limparCommand) {//GEN-END:|7-commandAction|1|38-preAction
                // write pre-action user code here
                Limpar();
//GEN-LINE:|7-commandAction|2|38-postAction
                // write post-action user code here
            } else if (command == sairCommand) {//GEN-LINE:|7-commandAction|3|73-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|73-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|46-preAction
        } else if (displayable == formApresentacao) {
            if (command == calcularCommand) {//GEN-END:|7-commandAction|5|46-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|6|46-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|7|41-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|8|41-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction
        }//GEN-END:|7-commandAction|9|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|10|
//</editor-fold>//GEN-END:|7-commandAction|10|






//<editor-fold defaultstate="collapsed" desc=" Generated Getter: viagemtextField ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initialized instance of viagemtextField component.
     *
     * @return the initialized component instance
     */
    public TextField getViagemtextField() {
        if (viagemtextField == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            viagemtextField = new TextField("N\u00FAmero de viagens por dia", "", 32, TextField.NUMERIC);//GEN-BEGIN:|36-getter|1|36-postInit
            viagemtextField.setInitialInputMode("0");//GEN-END:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return viagemtextField;
    }
//</editor-fold>//GEN-END:|36-getter|2|
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: limparCommand ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initialized instance of limparCommand component.
     *
     * @return the initialized component instance
     */
    public Command getLimparCommand() {
        if (limparCommand == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            limparCommand = new Command("Limpar", Command.ITEM, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return limparCommand;
    }
//</editor-fold>//GEN-END:|37-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: formApresentacao ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initialized instance of formApresentacao component.
     *
     * @return the initialized component instance
     */
    public Form getFormApresentacao() {
        if (formApresentacao == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            formApresentacao = new Form("Carregue Certo", new Item[]{getDescricaoStringItem(), getSpacer2(), getSpacer3(), getBannerImageItem()});//GEN-BEGIN:|39-getter|1|39-postInit
            formApresentacao.addCommand(getExitCommand());
            formApresentacao.addCommand(getCalcularCommand());
            formApresentacao.setCommandListener(this);//GEN-END:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return formApresentacao;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|40-getter|2|
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: calcularCommand ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initialized instance of calcularCommand component.
     *
     * @return the initialized component instance
     */
    public Command getCalcularCommand() {
        if (calcularCommand == null) {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            calcularCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|45-getter|1|45-postInit
            // write post-init user code here
        }//GEN-BEGIN:|45-getter|2|
        return calcularCommand;
    }
//</editor-fold>//GEN-END:|45-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: descricaoStringItem ">//GEN-BEGIN:|53-getter|0|53-preInit
    /**
     * Returns an initialized instance of descricaoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getDescricaoStringItem() {
        if (descricaoStringItem == null) {//GEN-END:|53-getter|0|53-preInit
            // write pre-init user code here
            descricaoStringItem = new StringItem("", "N\u00E3o perca mais tempo fazendo c\u00E1lculos, o aplicativo Carregue Certo faz para voc\u00EA, basta informar os valores, que o aplicativo informar\u00E1 quanto voc\u00EA dever\u00E1 colocar em seu cart\u00E3o transporte.");//GEN-BEGIN:|53-getter|1|53-postInit
            descricaoStringItem.setFont(getFont());//GEN-END:|53-getter|1|53-postInit
            // write post-init user code here
        }//GEN-BEGIN:|53-getter|2|
        return descricaoStringItem;
    }
//</editor-fold>//GEN-END:|53-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: font ">//GEN-BEGIN:|54-getter|0|54-preInit
    /**
     * Returns an initialized instance of font component.
     *
     * @return the initialized component instance
     */
    public Font getFont() {
        if (font == null) {//GEN-END:|54-getter|0|54-preInit
            // write pre-init user code here
            font = Font.getDefaultFont();//GEN-LINE:|54-getter|1|54-postInit
            // write post-init user code here
        }//GEN-BEGIN:|54-getter|2|
        return font;
    }
//</editor-fold>//GEN-END:|54-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer2 ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initialized instance of spacer2 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer2() {
        if (spacer2 == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            spacer2 = new Spacer(16, 1);//GEN-LINE:|55-getter|1|55-postInit
            // write post-init user code here
        }//GEN-BEGIN:|55-getter|2|
        return spacer2;
    }
//</editor-fold>//GEN-END:|55-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer3 ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initialized instance of spacer3 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer3() {
        if (spacer3 == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            spacer3 = new Spacer(16, 1);//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return spacer3;
    }
//</editor-fold>//GEN-END:|56-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: bannerImageItem ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initialized instance of bannerImageItem component.
     *
     * @return the initialized component instance
     */
    public ImageItem getBannerImageItem() {
        if (bannerImageItem == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            bannerImageItem = new ImageItem("", getLogoimage(), ImageItem.LAYOUT_DEFAULT, "carregue certo", Item.PLAIN);//GEN-LINE:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return bannerImageItem;
    }
//</editor-fold>//GEN-END:|57-getter|2|
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: logoimage ">//GEN-BEGIN:|58-getter|0|58-preInit
    /**
     * Returns an initialized instance of logoimage component.
     *
     * @return the initialized component instance
     */
    public Image getLogoimage() {
        if (logoimage == null) {//GEN-END:|58-getter|0|58-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|58-getter|1|58-@java.io.IOException
                logoimage = Image.createImage("/images/carreguecerto.png");
            } catch (java.io.IOException e) {//GEN-END:|58-getter|1|58-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|58-getter|2|58-postInit
            // write post-init user code here
        }//GEN-BEGIN:|58-getter|3|
        return logoimage;
    }
//</editor-fold>//GEN-END:|58-getter|3|
    //</editor-fold>





//<editor-fold defaultstate="collapsed" desc=" Generated Getter: sairCommand ">//GEN-BEGIN:|72-getter|0|72-preInit
    /**
     * Returns an initialized instance of sairCommand component.
     *
     * @return the initialized component instance
     */
    public Command getSairCommand() {
        if (sairCommand == null) {//GEN-END:|72-getter|0|72-preInit
            // write pre-init user code here
            sairCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|72-getter|1|72-postInit
            // write post-init user code here
        }//GEN-BEGIN:|72-getter|2|
        return sairCommand;
    }
//</editor-fold>//GEN-END:|72-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public void itemStateChanged(Item item) {
   
        try{
            
            // Declarando as variáveis  e atribuindo a elas os valores da caixa de texto
            double valor = Double.parseDouble(valorTextField.getString()) ;
            double quant = Double.parseDouble(quantidadeTextField.getString()) ;
            double viagem =Double.parseDouble(viagemtextField.getString());
            double r = 0;
                         
            if(estudanteChoiceGroup.isSelected(0)){ // verifica se é estudante
                r = (valor/2)*viagem*quant;
            }else if(estudanteChoiceGroup.isSelected(1)){ 
                r = valor*viagem*quant;
            }
           
            resultadoStringItem.setText(formatacao(r)); // Exibindo o resultado
        }catch(Exception ex){
            resultadoStringItem.setText("-");

        }        
    }
}
